<%@ include file="common.jsp"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
body {
	overflow-x: hidden;
	overflow-y: hidden;
	padding: 15px 15px 0 0;
}
</style>
</head>
<body>
	<form class="form-horizontal">
		<div class="form-group">
			<label class="col-sm-2 control-label">卡号：</label>
			<div class="col-sm-10">
				<input type="text" class="form-control" name="cid">
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label">积分：</label>
			<div class="col-sm-4">
				<div class="input-group">
					<span class="input-group-addon" id="basic-addon">+</span>
					<input type="text" class="form-control" aria-describedby="basic-addon" name="sum">
					<!--<span class="input-group-addon">.00</span>-->
				</div>
			</div>
		</div>
		<div class="form-group">
			<div class="col-sm-1 col-sm-offset-2">
				<button class="btn btn-default" type="button" onclick="update()">保存</button>
			</div>
			<div class="col-sm-1">
				<button class="btn btn-default" type="button" onclick="closeLayer()">返回</button>
			</div>
		</div>
	</form>
</body>
<script type="text/javascript">
	var card;
	var $cid = $('input[name="cid"]');
	var $sum = $('input[name="sum"]');

	$(function() {
		switch (parent.model) {
			case 1:
				$('#basic-addon').html('+');
				break;
			case -1:
				$('#basic-addon').html('-');
				break;
		}
	});

	function update() {
		if (validate()) {
			var url = BaseUrl + 'membershipCardAction/updateIntegralById.action';
			var option = {};
			option['cid'] = $cid.val();
			option['integral'] = parent.model == -1 ? $sum.val() * -1 : $sum.val();
			$.a(url, option, function(res) {
				console.log(res);
				if (res.result) {
					parent.layer.msg('保存成功', {
						time : 2000
					});
					closeLayer();
				} else {
					parent.layer.msg(res.message, {
						time : 2000
					});
				}
			}, false);
		}
	}

	function validate() {
		if (isEmpty($cid.val())) {
			layer.msg('卡号不能为空', {time : 2000});
			$cid.focus();
			return false;
		}
		if (isEmpty($sum.val())) {
			layer.msg('积分不能为空', {time : 2000});
			$sum.focus();
			return false;
		}
		if (isNaN($sum.val())) {
			layer.msg('积分必须为数字', {time : 2000});
			$sum.focus();
			return false;
		}
		if ($sum.val() < 0) {
			layer.msg('积分必须大于0', {time : 2000});
			$sum.focus();
			return false;
		}
		return true;
	}

	function closeLayer() {
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);
	}
</script>
</html>